//
//  SECPassModifyViewController.swift
//  aj
//
//  Created by leeqb on 12/13/15.
//  Copyright © 2015 powerlee. All rights reserved.
//

import UIKit

class SECPassModifyViewController: CBBaseTableViewController {
    @IBOutlet weak var oldPassField: UITextField!
    @IBOutlet weak var newPassField: UITextField!
    @IBOutlet weak var confirmPassField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.tableView.zeroInsets()
        self.tableView.delaysContentTouches = false
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.zeroInsets()
    }

    @IBAction func modifyPassClick(sender: CBButton) {
        let oldPass = oldPassField.text!
        let newPass = newPassField.text!
        let confirmPass = confirmPassField.text!
        
        if oldPass.isEmpty {
            CBAlertView.shareInstance().showMsg("请输入旧密码")
            return
        }
        
        if newPass.isEmpty {
            CBAlertView.shareInstance().showMsg("请输入新密码")
            return
        }
        
        if confirmPass.isEmpty {
            CBAlertView.shareInstance().showMsg("请输入确认密码")
            return
        }
        
        if newPass != confirmPass {
            CBAlertView.shareInstance().showMsg("密码不一致")
            return
        }
        
        let params: [NSObject : AnyObject] = ["userId": userId,
            "oldPassword": oldPass,
            "newPssword": newPass]
        CBMessage.shareInstance().showProcessing()
        AJNetworkHelper.instance.post("aj_update_pwd", parameters: params, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
            if responseObject["returnCode"] as? String == RetCodeSuccess {
                CBAlertView.shareInstance().showMsg("修改成功")
                self.navigationController?.popViewControllerAnimated(true)
            } else {
                CBAlertView.shareInstance().showMsg(responseObject["errorMsg"] as? String)
            }
            }, failed: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
            }) { () -> Void in
                CBMessage.shareInstance().dismiss()
        }
    }
}
